package com.ash.filter;

import com.ash.entity.Constant;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2020/12/26 11:42
 * @file : test06.${PACKAGE_NAME}.${NAME}.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@WebFilter("/jsp/*")
public class SystemFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("来到系统监听器");
        HttpServletRequest req1 = (HttpServletRequest) req;
        HttpServletResponse resp1 = (HttpServletResponse) resp;

        Object userSession = req1.getSession().getAttribute(Constant.USER_SESSION);
        if(userSession==null){
            System.out.println("重定向到error");
            /*过滤器不写斜杠会默认定位到过滤的路径下面*/
            resp1.sendRedirect("../error.jsp");
        }
        else {
            chain.doFilter(req, resp);
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
